package com.ruoyi.yljf.service.impl;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.system.domain.vo.SysDictDataVO;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.yljf.domain.bo.ResBO;
import com.ruoyi.yljf.domain.dto.DoctorDTO;
import com.ruoyi.yljf.domain.vo.CommentVO;
import com.ruoyi.yljf.domain.vo.DrugResVO;
import com.ruoyi.yljf.domain.vo.WelfareAreaTypeVO;
import com.ruoyi.yljf.domain.vo.CommonVO;
import com.ruoyi.yljf.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yljf.mapper.DoctorMapper;
import com.ruoyi.yljf.domain.Doctor;
import com.ruoyi.yljf.service.IDoctorService;

import javax.annotation.Resource;

/**
 * 医师Service业务层处理
 * 
 * @author metaee
 * @date 2025-02-14
 */
@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements IDoctorService {

    @Resource
    private DoctorMapper doctorMapper;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private CommonUtils utils;

    /**
     * 查询医师列表
     * 
     * @param doctor 医师
     * @return 医师
     */
    @Override
    public List<Doctor> selectDoctorList(Doctor doctor) {
    	LambdaQueryWrapper<Doctor> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotEmpty(doctor.getDoctorName())) {
			queryWrapper.like(Doctor::getDoctorName, doctor.getDoctorName());
		}
        if (StringUtils.isNotEmpty(doctor.getDoctorType())) {
			queryWrapper.eq(Doctor::getDoctorType, doctor.getDoctorType());
		}
        return doctorMapper.selectList(queryWrapper);
    }

    @Override
    public LinkedList<DrugResVO> queryDoctorList(DoctorDTO doctor) {
        List<Doctor> doctors = doctorMapper.selectList(
                new LambdaQueryWrapper<Doctor>()
                        .eq(StringUtils.isNotEmpty(doctor.getDoctorType())
                                        && !doctor.getDoctorType().equals("all"),
                                Doctor::getDoctorType, doctor.getDoctorType())
                        .like(StringUtils.isNotEmpty(doctor.getDoctorName()),
                                Doctor::getDoctorName, doctor.getDoctorName())
        );
        List<CommonVO> VOList = doctors.stream().map(item -> {
            CommonVO commonVO = new CommonVO();
            commonVO.setId(item.getDoctorId());
            commonVO.setName(item.getDoctorName());
            return commonVO;
        }).collect(Collectors.toList());
        LinkedList<DrugResVO> drugResVOS = utils.ListsortV2(VOList);

        return drugResVOS;

    }

    @Override
    public List queryDoctorTypeList() {
        //医师派别
        List<SysDictDataVO> courseType = dictDataService.selectDictDataList("doctor_type");
        ArrayList<WelfareAreaTypeVO> welfareAreaTypeVOS = new ArrayList<>();
        WelfareAreaTypeVO vo1 = new WelfareAreaTypeVO();
        vo1.setLabel("派别");
        vo1.setValue("doctor_type");
        List<ResBO> bo1 = new ArrayList<>();
        courseType.forEach(item ->{
            ResBO resBO1 = new ResBO(item.getLabel(), item.getValue());
            bo1.add(resBO1);
        });
        vo1.setList(bo1);
        welfareAreaTypeVOS.add(vo1);
        return welfareAreaTypeVOS;
    }

    @Override
    public List queryDoctorTypeListV2() {
        //医师派别
        List<SysDictDataVO> courseType = dictDataService.selectDictDataList("doctor_type");
        LinkedList<WelfareAreaTypeVO> welfareAreaTypeVOS = new LinkedList<>();
        WelfareAreaTypeVO vo1 = new WelfareAreaTypeVO();
        vo1.setLabel("派别");
        vo1.setValue("doctor_type");
        List<ResBO> bo1 = new ArrayList<>();
        ResBO resBO = new ResBO("全部","all");
        bo1.add(resBO);
        courseType.forEach(item ->{
            ResBO resBO1 = new ResBO(item.getLabel(), item.getValue());
            bo1.add(resBO1);
        });
        vo1.setList(bo1);
        welfareAreaTypeVOS.add(vo1);
        return welfareAreaTypeVOS;
    }
}
